<?php
class C_Cmsclass extends C_Abstract
{
    protected $_id = 0;
    protected $_space = '';
    protected $_idx = '';
    protected $_table = 't_cms_class';
    
    public function __construct()
    {
        parent::__construct();
        $this->_id = getRequest('id', '0', 'int');
        $this->_space = getRequest('space', '', 'stringTrim');
        $this->_idx = getRequest('idx', '', 'stringTrim');
    }
    
    public function indexAction()
    {
        $sql = 'SELECT SQL_CALC_FOUND_ROWS id, space, idx, cname, status FROM '. $this->_table .'  ORDER BY sort DESC '. $this->_limit;
        $data = $this->db->fetchAll($sql);
        $count = $this->getCount();
        $page_html = '';
        if ($count > $this->_perpage) {
            $PG = new \SF\Widgets\Paginator($this->_page, $count, $this->_perpage);
            $page_html = $PG->getRender();
            //$smarty->assign_by_ref('pages', $PG->getPages());
        }
        $this->smt->assign('list', $data);
        $this->smt->assign('page_html', $page_html);
        $this->smt->display('cms/class_list.html');
    }
    
    public function editAction()
    {
        if ($this->_id) {
            $sql = 'SELECT * FROM '. $this->_table .' WHERE id="'. $this->_id .'"';
            $info = $this->db->fetchRow($sql);
            $info['settings'] = json_decode($info['settings'], true);
            $this->smt->assign('info', $info);
        }
        $this->smt->display('cms/class_edit.html');
    }
    
    public function delAction()
    {
        if ($this->_id) {
            $sql = 'SELECT space, idx FROM '. $this->_table .' WHERE id="'. $this->_id .'"';
            $ic = $this->db->fetchRow($sql);
            if ($ic) {
                $sql = 'SELECT COUNT(*) FROM t_cms_link WHERE space="'. $ic['space'] .'" AND idx="'. $ic['idx'] .'"';
                $has_item = $this->db->fetchOne($sql);
                if ($has_item) {
                    alert('删除失败，请先删除此分类下的内容');
                }
            }
            $this->db->delete($this->_table, array('id=?'=>$this->_id));
        }
        \SF\Controller\Router::redirect('/?c=cmsclass');
    }
    
    public function saveAction()
    {
        $data = array(
            'space' => $this->_space,
            'idx' => $this->_idx,
            'cname' => getPost('cname', '', 'stringTrim'),
            'status' => 1,
            'sort' => time(),
        );

        $settings = array(
            'is_content' => getPost('item_is_content', '0', 'int'),
            'is_sort' => getPost('item_is_sort', '1', 'int'),
            'i_subtitle' => array(
                'name' => getPost('item_subtitle_name', '', 'stringTrim'),
                'flag' => getPost('item_subtitle_flag', '0', 'int'),
                'must' => getPost('item_subtitle_must', '0', 'int'),
                'note' => getPost('item_subtitle_note', '', 'stringTrim'),
            ),
            'i_link' => array(
                'name' => getPost('item_link_name', '', 'stringTrim'),
                'flag' => getPost('item_link_flag', '0', 'int'),
                'must' => getPost('item_link_must', '0', 'int'),
                'note' => getPost('item_link_note', '', 'stringTrim'),
            ),
            'i_img' => array(
                'name' => getPost('item_img_name', '', 'stringTrim'),
                'flag' => getPost('item_img_flag', '0', 'int'),
                'must' => getPost('item_img_must', '0', 'int'),
                'note' => getPost('item_img_note', '', 'stringTrim'),
                'deal' => getPost('item_img_deal', '', 'stringTrim'),
                'width' => getPost('item_img_width', '0', 'int'),
                'height' => getPost('item_img_height', '0', 'int'),
            ),
            'thumb' => array(
                'has' => getPost('item_thumb_has', '0', 'int'),
                'width' => getPost('item_thumb_width', '0', 'int'),
                'height' => getPost('item_thumb_height', '0', 'int'),
            ),
        );
        
        $data['settings'] = json_encode($settings);
        
        $this->db->save('t_cms_class', $data, array('id=?' => $this->_id));
        \SF\Controller\Router::redirect('/?c=cmsclass');
    }
}